#!/bin/bash

set -e

UBUNTU=false
DEBIAN=false
if [ "$(uname)" = "Linux" ]; then
  #LINUX=1
  if command -v apt-get >/dev/null; then
    OS_ID=$(lsb_release -is)
    if [ "$OS_ID" = "Debian" ]; then
      DEBIAN=true
    else
      UBUNTU=true
    fi
  fi
fi

# Check for non 64 bit ARM64/Raspberry Pi installs
if [ "$(uname -m)" = "armv7l" ]; then
  echo ""
  echo "WARNING:"
  echo "The script requires a 64 bit OS and this is 32 bit armv7l"
  echo "Exiting."
  exit 1
fi


UBUNTU_PRE_2004=false
if $UBUNTU; then
  LSB_RELEASE=$(lsb_release -rs)
  # In case Ubuntu minimal does not come with bc
  if ! command -v bc > /dev/null 2>&1; then
    sudo apt install bc -y
  fi
  # Mint 20.04 responds with 20 here so 20 instead of 20.04
  UBUNTU_PRE_2004=$(echo "$LSB_RELEASE<20" | bc)
  UBUNTU_2100=$(echo "$LSB_RELEASE>=21" | bc)
fi

install_python3_from_source_with_yum() {
  CURRENT_WD=$(pwd)
  TMP_PATH=/tmp


yum groupinstall -y "Development Tools"
 
yum install -y openssl-devel openssl libffi-devel bzip2-devel wget zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

  echo "cd $TMP_PATH"
  cd "$TMP_PATH"
 
  # yum install python3 brings Python3.6 which is not supported by chia
  cd ..

  wget https://www.python.org/ftp/python/3.9.11/Python-3.9.11.tgz
  tar xf Python-3.9.11.tgz
  cd Python-3.9.11
  echo "LD_RUN_PATH=/usr/local/lib ./configure --prefix=/usr/local"
  # '| stdbuf ...' seems weird but this makes command outputs stay in single line.
  LD_RUN_PATH=/usr/local/lib ./configure --prefix=/usr/local 
  #| stdbuf -o0 cut -b1-"$(tput cols)" | sed -u 'i\\o033[2K' | stdbuf -o0 tr '\n' '\r'; echo

  LD_RUN_PATH=/usr/local/lib make -j"$(nproc)" 

  LD_RUN_PATH=/usr/local/lib  make altinstall 
  cd "$CURRENT_WD"
}

# Manage npm and other install requirements on an OS specific basis
if [ "$(uname)" = "Linux" ]; then
  #LINUX=1
  if [ "$UBUNTU" = "true" ] && [ "$UBUNTU_PRE_2004" = "1" ]; then
    # Ubuntu
    echo "Installing on Ubuntu pre 20.04 LTS."
    sudo apt-get update
    sudo apt-get install -y python3.7-venv python3.7-distutils openssl
  elif [ "$UBUNTU" = "true" ] && [ "$UBUNTU_PRE_2004" = "0" ] && [ "$UBUNTU_2100" = "0" ]; then
    echo "Installing on Ubuntu 20.04 LTS."
    sudo apt-get update
    sudo apt-get install -y python3.8-venv python3-distutils openssl
  elif [ "$UBUNTU" = "true" ] && [ "$UBUNTU_2100" = "1" ]; then
    echo "Installing on Ubuntu 21.04 or newer."
    sudo apt-get update
    sudo apt-get install -y python3.9-venv python3-distutils openssl
  elif [ "$DEBIAN" = "true" ]; then
    echo "Installing on Debian."
    sudo apt-get update
    sudo apt-get install -y python3-venv openssl
  elif type pacman >/dev/null 2>&1 && [ -f "/etc/arch-release" ]; then
    # Arch Linux
    # Arch provides latest python version. User will need to manually install python 3.9 if it is not present
    echo "Installing on Arch Linux."
    case $(uname -m) in
      x86_64|aarch64)
        sudo pacman ${PACMAN_AUTOMATED} -S --needed git openssl
        ;;
      *)
        echo "Incompatible CPU architecture. Must be x86_64 or aarch64."
        exit 1
        ;;
    esac
  elif type yum >/dev/null 2>&1 && [ ! -f "/etc/redhat-release" ] && [ ! -f "/etc/centos-release" ] && [ ! -f "/etc/fedora-release" ]; then
    # AMZN 2
    echo "Installing on Amazon Linux 2."
    if ! command -v python3.9 >/dev/null 2>&1; then
      install_python3_from_source_with_yum
    fi
  elif type yum >/dev/null 2>&1 && [ -f "/etc/centos-release" ]; then
    # CentOS
    echo "Install on CentOS."
    if ! command -v python3.9 >/dev/null 2>&1; then
      install_python3_from_source_with_yum
    fi
  elif type yum >/dev/null 2>&1 && [ -f "/etc/redhat-release" ] && grep Rocky /etc/redhat-release; then
    echo "Installing on Rocky."
    # TODO: make this smarter about getting the latest version
    sudo yum install --assumeyes python39 openssl
  elif type yum >/dev/null 2>&1 && [ -f "/etc/redhat-release" ] || [ -f "/etc/fedora-release" ]; then
    # Redhat or Fedora
    echo "Installing on Redhat/Fedora."
    if ! command -v python3.9 >/dev/null 2>&1; then
      sudo yum install -y python39 openssl
    fi
  fi
elif [ "$(uname)" = "Darwin" ]; then
  echo "Installing on macOS."
  if ! type brew >/dev/null 2>&1; then
    echo "Installation currently requires brew on macOS - https://brew.sh/"
    exit 1
  fi
  echo "Installing OpenSSL"
  brew install openssl
elif [ "$(uname)" = "OpenBSD" ]; then
  export MAKE=${MAKE:-gmake}
  export BUILD_VDF_CLIENT=${BUILD_VDF_CLIENT:-N}
elif [ "$(uname)" = "FreeBSD" ]; then
  export MAKE=${MAKE:-gmake}
  export BUILD_VDF_CLIENT=${BUILD_VDF_CLIENT:-N}
fi

find_python() {
  set +e
  unset BEST_VERSION
  for V in 39 3.9 38 3.8 37 3.7 3; do
    if command -v python$V >/dev/null; then
      if [ "$BEST_VERSION" = "" ]; then
        BEST_VERSION=$V
        if [ "$BEST_VERSION" = "3" ]; then
          PY3_VERSION=$(python$BEST_VERSION --version | cut -d ' ' -f2)
          if [[ "$PY3_VERSION" =~ 3.10.* ]]; then
            echo "Chia requires Python version <= 3.9.10"
            echo "Current Python version = $PY3_VERSION"
            # If Arch, direct to Arch Wiki
            if type pacman >/dev/null 2>&1 && [ -f "/etc/arch-release" ]; then
              echo "Please see https://wiki.archlinux.org/title/python#Old_versions for support."
            fi
            exit 1
          fi
        fi
      fi
    fi
  done
  echo $BEST_VERSION
  set -e
}

if [ "$INSTALL_PYTHON_VERSION" = "" ]; then
  INSTALL_PYTHON_VERSION=$(find_python)
fi

# This fancy syntax sets INSTALL_PYTHON_PATH to "python3.7", unless
# INSTALL_PYTHON_VERSION is defined.
# If INSTALL_PYTHON_VERSION equals 3.8, then INSTALL_PYTHON_PATH becomes python3.8

INSTALL_PYTHON_PATH=python${INSTALL_PYTHON_VERSION:-3.7}

if ! command -v "$INSTALL_PYTHON_PATH" >/dev/null; then
  echo "${INSTALL_PYTHON_PATH} was not found"
  exit 1
fi

echo "Python version is $INSTALL_PYTHON_VERSION"